<template>
  <div class="a-login">
    <div class="b-login">
      <div class="c-login">
        <img src="../../assets/images/login/login2.png" alt="" />
      </div>
      <div class="d-login">
        <p>欢迎来到飞翔网吧</p>
        <el-form
          :model="loginfrom"
          ref="loginfrom"
          label-width="50px"
          class="demo-ruleForm"
          prefix-icon="el-icon-search"
          :rules="rules"
        >
          <el-form-item prop="username">
            <el-input
              type="username"
              v-model.number="loginfrom.username"
              autocomplete="off"
              placeholder="请输入账号"
              prefix-icon="el-icon-mobile-phone"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item prop="pwd">
            <el-input
              type="pwd"
              v-model.number="loginfrom.pwd"
              autocomplete="off"
              placeholder="请输入密码"
              prefix-icon="el-icon-unlock"
              show-password
            ></el-input>
          </el-form-item>
        </el-form>
        <el-button type="primary" @click="submitForm('loginfrom')">
          登录
        </el-button>
      
        <span>
          <router-link to="/register" class="e-login"
            >没有账号免费注册></router-link
          >
        </span>
      </div>
    </div>
  </div>
</template>

<script>
import { apiLoign } from "../../api/loginApi";
import { setToken } from "../../utils/cookies";
export default {
  data() {
    return {
      loginfrom: {
        username: "",
        pwd: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        pwd: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log("111");
          apiLoign(this.loginfrom).then((res) => {
            console.log(res);
            if (res.code == 200) {
              //cookie保存token
              setToken("token", res.data);
              //请求头添加token 在axios.js中添加了请求头
              //跳转首页
              this.$router.push("/");
            } else {
              alert("账户密码错误");
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.a-login {
  width: 100%;
  height: 100%;
  background: url("../../assets/images/login/login1.jpg");
  .b-login {
    width: 700px;
    height: 500px;
    background: white;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 50px;
    overflow: hidden;
    display: flex;
    box-shadow: 0 0 20px 15px hsl(48, 88%, 6%);
    background: hsla(40, 4%, 15%, 0.61);
    .c-login {
      flex: 1;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .d-login {
      flex: 1;
      p {
        margin: 60px 0;
        color: #3d8ffd;
        font-size: 30px;
        font-weight: bold;
        text-align: center;
      }
      /deep/.el-form {
        margin-right: 42px;
      }
      /deep/.el-button {
        width: 258px;
        margin-left: 12px;
        height: 50px;
        background: #3eb3e9;
        color: black;
        font-size: 16px;
        border: 0;
      }
      span {
        position: absolute;
        bottom: 50px;
        right: 20px;

        .e-login {
          text-decoration: none;
          color: #3eb3e9;
        }
      }
    }
  }
}
</style>
